/**
 * This code borrows heavily from DOM.js in the Firebug package.
 * See license.txt for terms of usage
 **/

FBL.ns(function()
{
    with ( FBL )
    {
        /* The hidden window trick for storing state. */
        var stateHolder = Components.classes["@mozilla.org/appshell/appShellService;1"].
            getService(Components.interfaces.nsIAppShellService).hiddenDOMWindow;

        /* On loading the page, see if the global version of the testFire object exists (it should). */
        var testFire = stateHolder.TestFire;

        try
        {

            function TestFireTreePanel()
            {
            }

            TestFireTreePanel.prototype = extend(TestFireTreePanelSupport,
            {

                getState: function()
                {
                    if ( !testFire.TreePanelState )
                    {
                        testFire.TreePanelState = {};
                    }
                    return testFire.TreePanelState;
                },

                destroyNode: function()
                {
                    this.panelNode.removeEventListener("mousedown", this.onMouseDown, false);
                    this.panelNode.removeEventListener("mouseover", this.onMouseOver, false);
                    this.panelNode.removeEventListener("mouseout", this.onMouseOut, false);
                },


                initialize: function( context, doc )
                {
                    try
                    {
                        this.onMouseDown = bind(this.onMouseDown, this);
                        this.onMouseOver = bind(this.onMouseOver, this);
                        this.onMouseOut = bind(this.onMouseOut, this);

                        /* 'super' call to initialize */
                        Firebug.Panel.initialize.apply(this, arguments);
                        addStyleSheet(this.panelNode.ownerDocument,
                                createStyleSheet(
                                    this.panelNode.ownerDocument, "chrome://TestFireExtension/content/TestFire.css"));

                        /* end 'super' call to initialize */
                        this.reInitialize();
                    }
                    catch( ex )
                    {
                        Firebug.TestFire.util.tf_debug(ex);
                    }
                },


                reInitialize: function(testset)
                {
                    try
                    {
                        var state = this.getState();
                        state.objectPath = [];
                        state.viewPath = [];
                        state.pathIndex = -1;
                        state.toggles = {};
                        state.initialized = true;
                        state.watches = [];



                        if(testFire.getTestset())
                        {
                            this.getState().selectedTargetNodeWrapper =
                                new TestFireObjectWrapper(null, "Nothing Selected", "Selected Object", true, testFire);
                            state.watches.push(this.getState().selectedTargetNodeWrapper);
                            state.watches.push(testFire.getTestset());
                        }
                        else{
                            var msg = new TestFireObjectWrapper(null, "Please open a valid URL", "", true);
                            state.watches.push(msg);
                        }
                        this._refresh();
                    }
                    catch( ex )
                    {
                        Firebug.TestFire.util.tf_debug(ex);
                    }

                },

                _refresh: function()
                {

                    if(Firebug.TestFire){
                        var testset = Firebug.TestFire.getTestset();  

                        if(testset){
                            this.updateSelection();
                        }
                    }
                },

                rebuild: function()
                {

                    if(Firebug.TestFire)
                        Firebug.TestFire.refreshAll();

                },



            /**
             * Called directly before the panel is hidden; this IS called on every change of page (Refresh, navigate
             * away from current page, etc). Not called when a tab is changed in the TestFire extension.
             */
                destroy: function( state )
                {

                },

                hide: function()
                {
                    var view = this.getState().viewPath[this.getState().pathIndex];
                    if ( view && this.panelNode.scrollTop )
                        view.scrollTop = this.panelNode.scrollTop;
                },



            /**
             * Called when the panel is displayed, after the call to initialize.
             *
             * I can't figure out why the state being passed in is null; it isn't even close to immediatly
             * obvious, and I need to move on for now.
             *
             *
             */
                show: function( state )
                {
                    try
                    {

                    }
                    catch( ex )
                    {
                        Firebug.TestFire.util.tf_debug(ex)
                    }

                },
            // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
            // extends Panel


                name: "TestFireTreePanel",
                title: "Test Fire",
                searchable: false,
                editable: true,
                lastTestSet: null,
                asString: '',

                order: 0


            })
            // ************************************************************************************************

            Firebug.registerPanel(TestFireTreePanel);

            // ************************************************************************************************
        }
        catch( e )
        {
            Firebug.TestFire.util.tf_debug(e);
        }

    }
});
